package com.tanhua.server.service;

import cn.hutool.core.util.StrUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.tanhua.common.pojo.User;
import com.tanhua.common.service.PicUploadService;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.common.vo.PicUploadResult;
import com.tanhua.dubbo.server.api.VideoApi;
import com.tanhua.dubbo.server.pojo.Video;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @program: my-tanhua
 * @description:  都是在service中调用service的方法
 * @author: xuejingjing
 * @create: 2021-10-04 19:12
 **/
@Service
public class VideoService {

    @Reference
    private VideoApi videoApi;

    //图片上传到oss
    @Autowired
    private PicUploadService picUploadService;

    @Autowired
    private FastFileStorageClient fastFileStorageClient;

    @Autowired
    private FdfsWebServer fdfsWebServer;

    /**
     * 小视频上传
     * @param picFile 图片封面
     * @param videoFile  视频文件
     * @return
     */
    public Boolean saveVideo(MultipartFile picFile, MultipartFile videoFile) throws IOException {
        Video video = new Video();
        User user = UserThreadLocal.get();
        video.setUserId(user.getId());
        video.setSeeType(1);// 谁可以看，1-公开，2-私密，3-部分可见，4-不给谁看
        //上传图片到阿里云oss
        PicUploadResult upload = picUploadService.upload(picFile);

        //上传视频到fastdfs
        StorePath storePath = fastFileStorageClient.uploadFile(videoFile.getInputStream(),
                videoFile.getSize(),
                //aa-bb.cc.mp4
                StrUtil.subAfter(videoFile.getOriginalFilename(), ".", true),
                null);

         video.setVideoUrl(fdfsWebServer.getWebServerUrl() + storePath.getFullPath());

           String videoId = videoApi.saveVideo(video);
           return StrUtil.isNotBlank(videoId);
    }
}